package com.lwl.Algorithmic_data_structure.class14贪心算法;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author lwl
 * @Description 给出一个 资本，利润 的二维数组，以及一个初始资金和可以完成的项目数，求赚取的最大利润
 * 你只有达到资本才能做这个项目，不需要花费金钱
 * @date 2023/6/1 11:13
 */
public class Code02getMaxProfits {

    class Project {

        int cost;

        int profit;
    }

    public int getMaxProfits(Project[] projects, int money, int count) {
        PriorityQueue<Project> minCostHeap = new PriorityQueue<>(new Comparator<Project>() {
            @Override
            public int compare(Project o1, Project o2) {
                return o1.cost - o2.cost;
            }
        });
        PriorityQueue<Project> maxProfitHeap = new PriorityQueue<>(new Comparator<Project>() {
            @Override
            public int compare(Project o1, Project o2) {
                return o2.profit - o1.profit;
            }
        });
        minCostHeap.addAll(Arrays.asList(projects));
        for (int i = 0; i < count; i++) {
            while (!minCostHeap.isEmpty() && minCostHeap.peek().cost <= money) {
                maxProfitHeap.add(minCostHeap.poll());
            }
            if (maxProfitHeap.isEmpty()) {
                break;
            }
            Project project = maxProfitHeap.poll();
            money += project.profit;
        }
        return money;
    }

}
